package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/travel")
public class TravelController {
    @Autowired
    ITravelService travelService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") TravelQueryObject qo) {
        //默认查询状态为待审核的游记对象,并按更新时间升序显示
        if (qo.getState() == null) {
            qo.setState(1);
        }
        qo.setOrderBy("t.lastUpdateTime");
        PageInfo<Travel> pageInfo = travelService.queryForList(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "/travel/list";
    }

    @RequestMapping("/releaseList")
    public String releaseList(Model model, @ModelAttribute("qo") TravelQueryObject qo) {
        qo.setState(2);
        qo.setOrderBy("t.releaseTime DESC");
        PageInfo<Travel> pageInfo = travelService.queryForList(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "/travel/releaseList";
    }

    @RequestMapping("/getContentById")
    @ResponseBody
    public Travel getContentById(Long id) {
        return travelService.get(id);
    }

    @RequestMapping("/updateState")
    @ResponseBody
    public void updateState(Travel travel) {
        travelService.updateState(travel);
    }

}
